package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.MbrMemberHistory;
import com.macro.mall.portal.service.MbrMemberHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Map;

/**
 * MbrMemberHistory的路由接口服务
 * 
 * @author 
 *
 */
@Slf4j
@RestController
@RequestMapping("history")
@Api(tags = "MbrMemberHistoryController", description = "会员课程历史表(mbr_member_history）")
public class MbrMemberHistoryController {

	/** MbrMemberHistoryService服务 */
	@Autowired
	private MbrMemberHistoryService mbrMemberHistoryService;



    @ApiOperation("用户历史列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> list(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        Map<String, Object> mbrMemberHistories = mbrMemberHistoryService.find(new MbrMemberHistory());
        return CommonResult.success(mbrMemberHistories);
    }

    @ApiOperation("用户历史数量")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Long> count(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        long count = mbrMemberHistoryService.count(new MbrMemberHistory());
        return CommonResult.success(count);
    }

    @ApiOperation("添加")
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult save(Principal principal, @ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            if(principal==null){
                return CommonResult.unauthorized(null);
            }
            mbrMemberHistoryService.saveNotNull(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) String id) {
        try {
            mbrMemberHistoryService.deleteById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
